package com.ac.cv.client.menu;

import java.util.List;

import com.ac.cv.client.KeyWord;
import com.ac.cv.client.KeyWordsWidget;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class PopupLabel extends ClickableLabel {

	private DecoratedPopupPanel simplePopup;

	private int id;

	public PopupLabel(String title, String content, int id) {
		super(title);
		this.id = id;
		
		this.addStyleName("selected");
		this.addStyleName("selectable");
		
		simplePopup = new DecoratedPopupPanel(true);
		simplePopup.addStyleName("selectable");
		Label contentLabel = new Label(content);
		contentLabel.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				simplePopup.hide();
			}

		});
		simplePopup.setWidget(contentLabel);
		KeyWordsWidget.getInstance().addKeyWord(this);
	}

	@Override
	public void onClick(ClickEvent event) {
		super.onClick(event);
		if (!simplePopup.isShowing()) {
			Widget source = (Widget) event.getSource();
			int left = source.getAbsoluteLeft() + 10;
			int top = source.getAbsoluteTop() + 10;
			simplePopup.setPopupPosition(left, top);
			simplePopup.show();
		} else {
			simplePopup.hide();
		}
	}

	@Override
	public void onSelect() {
		List<KeyWord> listKeyWord = KeyWordsWidget.getInstance().getKeyWord(id);
		if (listKeyWord != null) {
			for (KeyWord keyword : listKeyWord) {
				if (keyword.isMainKeyWord()) {
					keyword.onSelect();
				}
			}
		}
		Timer t = new Timer() {

			@Override
			public void run() {
				simplePopup.setPopupPosition(PopupLabel.this.getAbsoluteLeft() + 10,
						PopupLabel.this.getAbsoluteTop() + 15);
				simplePopup.show();
			}

		};
		t.schedule(400);
	}

	@Override
	public int getId() {
		return id;
	}

	@Override
	public boolean isMainKeyWord() {
		return false;
	}

	
	
}
